import { notNilEmpty } from 'qx-util';
import { ModelInstanceHelp } from '../core';
import { IApiConfig } from '../core';
import { PSModelServiceImpl } from '../psmodel-service-impl';
import { ApiConfig } from './app-config';

/**
 * 全局帮助类
 *
 * @export
 * @class Global
 */
export class Global {
  /**
   * 默认实例帮助类
   *
   * @author chitanda
   * @date 2021-05-12 16:05:13
   * @protected
   * @static
   * @type {ModelInstanceHelp}
   */
  protected static defaultHelp: ModelInstanceHelp;

  /**
   * 安装默认实例数据
   *
   * @author chitanda
   * @date 2021-04-10 11:04:20
   * @static
   * @param {PSModelServiceImpl} modelService 指定模型根实例模型服务
   * @param {(modelPath: string) => Promise<IModel>} getModelJson 模型JSON文件请求
   * @return {*}  {Promise<ModelInstanceHelp>}
   */
  static async install(
    modelService: PSModelServiceImpl,
    getModelJson: (modelPath: string) => Promise<IModel>,
    config?: IApiConfig,
  ): Promise<ModelInstanceHelp> {
    if (config) {
      if (notNilEmpty(config.lang)) {
        // 多语言设置，并转换
        ApiConfig.lang = config.lang.replace('-', '_').toUpperCase();
      }
    }
    const help = new ModelInstanceHelp();
    await help.install(modelService, getModelJson);
    this.defaultHelp = help;
    return help;
  }

  /**
   * 安装沙箱实例数据
   *
   * @author chitanda
   * @date 2021-05-12 16:05:56
   * @static
   * @param {PSModelServiceImpl} modelService 指定模型根实例模型服务
   * @param {(modelPath: string) => Promise<IModel>} getModelJson 模型JSON文件请求
   * @return {*}  {Promise<ModelInstanceHelp>}
   */
  static async sandboxInstall(
    modelService: PSModelServiceImpl,
    getModelJson: (modelPath: string) => Promise<IModel>,
  ): Promise<ModelInstanceHelp> {
    const help = new ModelInstanceHelp();
    await help.install(modelService, getModelJson);
    return help;
  }

  /**
   * 根据实例id获取对应模型服务
   *
   * @author chitanda
   * @date 2022-12-23 15:12:05
   * @static
   * @return {*}  {PSModelServiceImpl}
   */
  static getModelService(): PSModelServiceImpl {
    return this.defaultHelp.getModelService();
  }
}
